package com.example.springdemo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: Hu Shan
 * @date: 2023/3/22 21:00
 * @description: TODO
 */
@RestControllerAdvice
public class GlobalErrorHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ErrorMessage> businessExcpResp(BusinessException e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-CUSTOM-ERROR", "500")
                .body(new ErrorMessage(e.getMessage(), false));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorMessage> errorResp(Exception e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-ERROR", "500")
                .body(new ErrorMessage(e.getMessage(), false));
    }
}